package cn.javaweb.group0.model;

import cn.javaweb.entity.User;
import cn.javaweb.group0.entity.Log;
import cn.javaweb.model.BaseModel;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LogModel extends BaseModel {
    public Object parseObject(ResultSet rs){

        Log log = new Log();

        try{
            log.setId(rs.getInt("id"));
            log.setIp(rs.getString("ip"));
            log.setCreatetime(rs.getDate("createtime"));
            log.setUrl(rs.getString("url"));
            log.setMethod(rs.getString("method"));
            log.setDesp(rs.getString("desp"));
            log.setUser(rs.getString("user"));
            return log;
        }catch (Exception e){

        }
        return null;

    }

    public List<Log> getLogList(User user, int pageNum, int pageLimit){

        String sql = "select * from log where user=? limit ?, ?";

        Object [] params = { user.getUsername() , pageNum, pageLimit };
        List<Object> list = this.selectAll(sql, params);

        List<Log> re = new ArrayList<Log>();

        for(Object one:list){
            re.add((Log)one);
        }
        return re;
    }

    public boolean addLog(String ip, String url, String method, String desp, String user){

        System.out.println(ip+","+url+","+method+","+desp+","+user);
        String sql = "insert into log (ip, url, method, desp, user, createtime) values ( ?,?,?,?,?, NOW())";
        Object [] params = { ip, url, method, desp, user};
        int r = update(sql, params);

        return r>0;
    }

    public int getTotal(){
        return getCount("select count(*) as cnt from log where 1", null);
    }
}
